/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.editparts;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

public class LoadOperatorFigure extends EditableFigure {

    private static final Color textColor = new Color(null, 255, 255, 206);
    private static final Font nameFont = new Font(null, "Arial", 12, SWT.BOLD);

    private Label nameLabel = makeNameLabel();
    // private CompartmentFigure textFigure = new CompartmentFigure(textColor,
    // new InsetLineBorder());
    private QueryFigure queryFigure = new QueryFigure(textColor,
            new InsetNoBorder());

    private CompartmentFigure aliasFigure = new CompartmentFigure(
            ColorConstants.white, new InsetNoBorder());

    // load query components
    private CompartmentFigure identifier = new CompartmentFigure(textColor,
            new InsetNoBorder());
    private Label opName = new Label();
    private CompartmentFigure fileName = new CompartmentFigure(textColor,
            new InsetNoBorder());
    private Label using = new Label();
    private CompartmentFigure loadFunc = new CompartmentFigure(textColor,
            new InsetNoBorder());
    private Label as = new Label();

    public LoadOperatorFigure() {
        ToolbarLayout layout = new ToolbarLayout();
        setLayoutManager(layout);

        layout.setVertical(true);

        setOpaque(false);

        // add the query components
        queryFigure.add(identifier);
        queryFigure.add(opName);
        queryFigure.add(fileName);
        queryFigure.add(using);
        queryFigure.add(loadFunc);
        queryFigure.add(as);

        add(nameLabel);
        add(queryFigure);
        add(aliasFigure);
    }

    public class InsetLineBorder extends LineBorder {
        public Insets getInsets(IFigure figure) {
            return new Insets(2, 4, 2, 4);
        }
    }

    public class InsetNoBorder extends AbstractBorder {
        public Insets getInsets(IFigure figure) {
            return new Insets(2, 4, 2, 4);
        }

        public void paint(IFigure figure, Graphics graphics, Insets insets) {
            // no-op
        }
    }

    private Label makeNameLabel() {
        Label l = new Label();
        l.setFont(nameFont);
        return l;
    }

    public void setName(String str) {
        if (str == null)
            str = "";
        nameLabel.setText(str);
        nameLabel.revalidate();
    }

    public void setAlias(String str) {
        if (str == null)
            str = "";
        aliasFigure.setText(str);
        aliasFigure.revalidate();
    }

}
